import com.asnike.P.bejeweled.Jewel;

import flash.filters.GlowFilter;

private function getSelectFilter( $data:Object ):BitmapFilter{
	return new GlowFilter( $data.color, $data.alpha, $data.blurX, $data.blurY, $data.strength, $data.quality );
}

internal function addJewel( $type:int, $x:Number, $y:Number ):Jewel{
	var jewel:Jewel;
	
	jewel = Jewel.GET( $type );
	jewel.x = $x;
	jewel.y = $y;
	jewel.buttonMode = true;
	addChild( jewel );
	
	return jewel;
}

internal function selectJewel( $target:Jewel ):void{
	addSelectEffect( $target );
}
private function addSelectEffect( $target:Jewel ):void{
	$target.filters = [SELECT_FILTER];
}

internal function unselectJewel( $target:Jewel ):void{
	removeSelectEffect( $target );
}
private function removeSelectEffect( $target:Jewel ):void{
	$target.filters = [];
}

internal function shiftJewels( $target1:Jewel, $target2:Jewel, $endFunction:Function ):void{
	TweenMax.to( $target1, JEWEL_SHIFT_DURATION, {ease:Circ.easeOut, x:$target2.x, y:$target2.y, onComplete:unselectJewel, onCompleteParams:[$target1]} );
	TweenMax.to( $target2, JEWEL_SHIFT_DURATION, {ease:Circ.easeOut, x:$target1.x, y:$target1.y, onComplete:shiftJewelsEnd, onCompleteParams:[$target2, $endFunction]} );
}
private function shiftJewelsEnd( $target:Jewel, $endFunction:Function ):void{
	unselectJewel( $target );
	$endFunction();
}

internal function removeJewels( $targets:Array ):void{
	var i:int, j:int;
	
	for( i = 0, j = $targets.length ; i < j ; ++i ){
		removeJewel( $targets[i] );
	}
}
internal function removeJewel( $target:Jewel ):void{
	TweenMax.to( $target, JEWEL_REMOVE_DURATION, {scaleX:.1, scaleY:.1, ease:Circ.easeOut, onComplete:removeChild, onCompleteParams:[$target]} );
}

internal function moveDownToJewel( $target:Jewel, $amount:int, $endFunction:Function ):void{
	//			TweenMax.to( $target, $amount*.15, {y:$target.y + Jewel.HEIGHT*$amount, ease:Quart.easeOut, onComplete:moveDownToJewelEnd, onCompleteParams:[$endFunction]} );
	TweenMax.to( $target, $amount*.1, {y:$target.y + Jewel.HEIGHT*$amount, ease:Linear.easeNone, onComplete:moveDownToJewelEnd, onCompleteParams:[$endFunction]} );
}
private function moveDownToJewelEnd( $endFunction:Function ):void{
	if( $endFunction is Function ){
		$endFunction();
	}
}